package com.yc.framework.web.exception;

import com.yc.common.annotation.Log;
import com.yc.common.core.domain.AjaxResult;
import com.yc.common.exceptions.user.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author YanChen
 * @Date 2025/4/18 7:02
 **/
@RestControllerAdvice
@Slf4j
public class UserExceptionHandler {
    /**
     * 用户异常
     */
    @Log
    @ExceptionHandler(UserException.class)
    public AjaxResult handleUserException(UserException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',用户操作异常: {}", requestURI, e.getMessage());
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 用户不存在
     */
    @Log
    @ExceptionHandler(UserNotExistsException.class)
    public AjaxResult handleUserNotExistsException(UserNotExistsException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',用户不存在: {}", requestURI, e.getMessage());
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 用户密码不匹配
     */
    @Log
    @ExceptionHandler(UserPasswordNotMatchException.class)
    public AjaxResult handleUserPasswordNotMatchException(UserPasswordNotMatchException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',用户密码不匹配: {}", requestURI, e.getMessage());
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 用户账号已禁用
     */
    @Log
    @ExceptionHandler(UserDisabledException.class)
    public AjaxResult handleUserDisabledException(UserDisabledException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',用户账号已禁用: {}", requestURI, e.getMessage());
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 用户账号已删除
     */
    @Log
    @ExceptionHandler(UserDeleteException.class)
    public AjaxResult handleUserDeleteException(UserDeleteException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',用户账号已删除: {}", requestURI, e.getMessage());
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 用户注册异常
     */
    @Log
    @ExceptionHandler(UserRegisterException.class)
    public AjaxResult handleUserRegisterException(UserRegisterException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',用户注册失败: {}", requestURI, e.getMessage());
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * 用户密码重试次数超限
     */
    @Log
    @ExceptionHandler(UserPasswordRetryLimitExceedException.class)
    public AjaxResult handleUserPasswordRetryLimitExceedException(UserPasswordRetryLimitExceedException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',用户密码重试次数超限: {}", requestURI, e.getMessage());
        return AjaxResult.error(e.getCode(), e.getMessage());
    }
}
